package cn.quevo.ucenter.client.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import cn.quevo.core.entity.UserInfoDetails;
import cn.quevo.core.exception.CustomException;
import cn.quevo.ucenter.client.service.ClientRestService;
import cn.quevo.ucenter.core.annotion.OAuthToken;

/**
 * 权限验证拦截器抽象实现
 * 
 * @param <T>
 * @author July july_sky@foxmail.com
 * @date 2017年12月29日 下午5:57:07
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @version 1.0
 */
public class AuthorizedHandlerInterceptor<T extends UserInfoDetails> extends AbstractAuthorizedHandlerInterceptor<T>
        implements HandlerInterceptor {

    public AuthorizedHandlerInterceptor(ClientRestService clientRestService) {
        super(clientRestService);
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws CustomException {
        // 验证Handler类型
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        OAuthToken oAuthToken = handlerMethod.getMethodAnnotation(OAuthToken.class);
        if (oAuthToken == null) {
            return true;
        }
        super.getUserInfo(request);
        return true;
    }
}
